<?php
	include 'user.php';
	
	class Message
	{
		public $messageText;
		public $company;
		
		function __construct($withMessageText=null, $fromCompany=null){
			$this->messageText=$withMessageText;
			$this->company=$fromCompany;
		}
		
	}
	
	function getMessages($fromCompany=null) {
		$mysql_host = "mysql16.000webhost.com";
		$mysql_database = "a1863493_venture";
		$mysql_user = "a1863493_teamven";
		$mysql_password = "teamventure1";
		
		$connection = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die ("<p>Failed to Connect to Database</p>");
		
		mysql_select_db($mysql_database, $connection) or die ("<p>Failed to connect to database2</p>");
		
		$query = mysql_query("SELECT message, idbus_acct FROM message WHERE message.idbus_acct = '$fromCompany'");
		$messages = array();
		while($row = mysql_fetch_row($query)) {
			$newMessage = new Message($row['message'], $row['idbus_acct']);
			array_push($messages, $newMessage);
		}
		
		mysql_close($connection);
		return $messages;
	}
	
	function saveMessage($withMessageText=null, $fromCompany=null){
		//$connection = mysql_connect($mysql_host, 'a1863493_teamven', 'teamventure1');
		//mysql_select_db($mysql_database, $connection) or die ("<p>Failed to connect to database2</p>");
		
		$sql="INSERT INTO message (message, idbus_acct) VALUES ('$withMessageText', '$fromCompany')";
		if(!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		}
		
		//mysql_close($connection);
	}
	
	function sendMessage($withMessageID) {
		$mysql_host = "mysql16.000webhost.com";
		$mysql_database = "a1863493_venture";
		$mysql_user = "a1863493_teamven";
		$mysql_password = "teamventure1";
	
		$connection = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die ("<p>Failed to Connect to Database</p>");

		mysql_select_db($mysql_database, $connection) or die ("<p>Failed to connect to database2</p>");
		
		
		$query="SELECT cust_acct.idcust_acct, phone, supported_carriers.name, email, byphone, byemail, bytwitter, byfacebook 
			FROM cust_acct, contact_preference, bus_cust, supported_carriers, message 
			WHERE  message.idmessage = '1'
			AND cust_acct.idcust_acct = bus_cust.idcust_acct 
			AND message.idbus_acct = bus_cust.idbus_acct
			AND supported_carriers.idSupported_carriers = cust_acct.idSupported_carriers
			OR cust_acct.idSupported_carriers = NULL
			GROUP BY cust_acct.idcust_acct";
			
		$queryresult = mysql_query($query);
		$users = array();
		while($row = mysql_fetch_assoc($queryresult)) {
			$newUser = new User($row['idcust_acct'], $row['phone'], $row['name'], $row['email'], $row['byphone'], $row['byemail'], $row['bytwitter'], $row['byfacebook']);
			array_push($users, $newUser);
		}
		echo "test";
		$return= mysql_query("SELECT message FROM message WHERE idmessage = '$withMessageID'");
		//$message = mysql_fetch_field($query, 0);
		if (mysql_num_rows($return)){
		$message = mysql_result($return,0);
		
		$query= mysql_query("SELECT message FROM message WHERE idmessage = '$withMessageID'");
		$message = mysql_result($query,0);
		
		foreach ($users as $user){
			if($user->byphone == 1){
				echo $user->phone;
				echo $user->carrier;
				sendPhoneMessage($message, $user->phone, $user->carrier);

			}
			if($user->byemail == 1){
				sendEmailMessage($message, $user->email);
				echo $user->email;
			}
			if($user->bytwitter == 1){
				sendTwitterMessage($message, $user->twitter);
			}
			if($user->byfacebook == 1){
			
			}
			
		}
		
		}
		header ('Location: ../bushome.html');
		mysql_close($connection);
		
	}
	
	function sendPhoneMessage($isMessage=null, $phoneNumber=null, $carrier=null){

		if ((empty($phoneNumber)) || (empty($isMessage))) {
			header ("Location: sms_error.php");
			echo "fail";
		}

		else if ($carrier == "verizon") {
			$formatted_number = $phoneNumber."@vtext.com";
			mail($formatted_number, "SMS", $isMessage);
			//header ("Location: sms_success.php");
		}

		else if ($carrier == "tmobile") {
			$formatted_number = $phoneNumber."@tomomail.net";
			mail($formatted_number, "SMS", $isMessage);
			//header ("Location: sms_success.php");
		}

		else if ($carrier == "sprint") {
			$formatted_number = $phoneNumber."@messaging.sprintpcs.com";
			mail($formatted_number, "SMS", $isMessage);
			//header ("Location: sms_success.php");
		}

		else if ($carrier == "att") {
			$formatted_number = $phoneNumber."@txt.att.net";
			mail($formatted_number, "SMS", $isMessage);
			//header ("Location: sms_success.php");
		}

		else if ($carrier == "virgin") {
			$formatted_number = $phoneNumber."@vmobl.com";
			mail($formatted_number, "SMS", $isMessage);
			//header ("Location: sms_success.php");
		}
	}
	
	function sendEmailMessage($isMessage=null, $withEmail=null){
		mail($withEmail, "SMS", $isMessage);
		
	}
	
	function sendTwitterMessage($isMessage=null, $isTwitter=null){
		
		$consumerKey    = 'I0f5WLMK2q1zKgFXkwVA';
		$consumerSecret = '76tYS9dOsHgLjByzCtKMYQoqGAoMClMxKhUzv4vA';
		$oAuthToken     = '228792641-PvH6W2Gsx6EL0tP5jIYAMTKi25f4CH1LIfqUaNbt';
		$oAuthSecret    = 'UygIF58ifYauLXhixiXN7tYEsHM2JAeubov8j5DBfY';
		 
		require_once('twitteroauth.php');
		 
		// create a new instance
		$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
		 
		//send a tweet
		$tweet->post('statuses/update', array('status' => '$isMessage'));
	
	}
	
	function sendFacebookMessage($isMessage=null, $isFacebook=null){
	
	}
?>